About 10141 letters
About 51 minutes
Arithmetic is playing around with values, like 1 + 1
gives 2
, where +
is the operator for addition.
For more information about operators, please refer to Python official documentation
Python uses =
as an assignment operator to modify the value of a variable, not "equals" in mathematics.
You can also use count += 1
as a shorthand for count = count + 1
, where +
can be replaced by any arithmetic operator.
count:int = 0 # Create a variable count with a value of 0
print(count)
count = 4 # Change the value of count to 4
print(count)
count += 3 # Change the value of count to count + 3
print(count)
After a variable is created in Python, it can be assigned other types of values:
age_of_yukari:int = 17 # type is int
age_of_yukari = "17" # type is str
However, it is recommended not to modify the type of the variable.
The walrus operator (:=
) is a special assignment operator that cannot be used to assign values to variables directly, but can only be used within expressions. For example:
x:int = 0
y:int = 0
sum_squares:int = x**2 + y**2
# traditional practice
x = 5
y = 10
sum_squares = x**2 + y**2
# use walrus operator
sum_squares = (x := 5)**2 + (y := 10)**2
Also, the assignment operator (=
) does not return a value and cannot be used as a condition for control blocks such as if
and while
, while the walrus operator (:=
) can.
operators | name | example | result | illustrate |
---|---|---|---|---|
+ | addition operator | 3 + 5 | 7 | |
- | subtraction operator | 5 - 3 | 2 | |
* | multiplication operator | 5 * 3 | 15 | |
/ | division operator | 5 / 2 | 2.5 | regardless of whether it is divisible or not, the result type is floating point |
// | integer division operator | 5 // 2 | 2 | the result is rounded down. If both the dividend and the divisor are integers, the result is an integer, otherwise the result is a floating point. |
% | remainder operator | 5 % 2 | 1 | 5 divided by 2 equals 2 with a remainder of 1 |
** | exponentiation operator | 5 ** 2 | 25 | 5 to the power of 2 |
Used to compare the relationship between two values, and the result is boolean type.
operators | name | example | result |
---|---|---|---|
== | equals operator | 3 == 5 | False |
!= | not equal to operator | 3 != 5 | True |
> | greater than operator | 5 > 3 | True |
< | less than operator | 5 < 2 | False |
>= | greater than or equal to operator | 5 >= 5 | True |
<= | less than or equal to operator | 5 <= 2 | False |
Used to combine multiple boolean values.
operators | name | example | result | illustrate |
---|---|---|---|---|
and | logical AND operator | True and False | False | The result is True if all are True , otherwise the result is False |
or | logical OR operator | True or False | True | The result is False if all are False , otherwise the result is True |
not | logical NOT operator | not False | True | Invert a noolean value |
Bitwise operations are used to perform binary operations on integers.
operators | name | example | result | illustrate |
---|---|---|---|---|
& | bitwise AND operator | 0b1100 & 0b0110 | 0b0100 | Calculate by binary digit. If both numbers have 1 in a certain digit, the result is 1 in that digit. Otherwise, the result is 0 in that digit. |
| | bitwise or operator | 0b1100 | 0b0110 | 0b1110 | Calculate by binary digit. If both numbers are 0 in a certain digit, the result is 0 in that digit. Otherwise, the result is 1 in that digit. |
^ | bitwise XOR operator | 0b1100 ^ 0b0110 | 0b1010 | Different bits in binary result in 1 , and the same bits result in 0 |
<< | left shift operator | 0b0011 << 2 | 0b1100 | Shift the binary bit to the left and fill the right side with 0 |
>> | right shift operator | 0b1100 >> 1 | 0b0110 | Shift the binary bit to the right and fill the left side with 0 |
~ | negation operator | ~0b1100 | 0b0011 | Invert a binary bit, i.e., 1 becomes 0 , and 0 becomes `1 |
Operators have a priority, just like in mathematics, multiplication and division are performed first, and addition and subtraction are performed later. For example,
Parentheses have the highest priority, and you can use them to change the order of calculation. For example,
The following table is a list of precedences, from high to low:
Reference: Python official documentation
operators | illustrate | priority |
---|---|---|
(exp) , [exp] , {exp} | Binding or parenthesized expression, list display, dictionary display, set display | highest |
arr[i] , arr[i:j] , func(args) , obj.attr | Subscription, slicing, call, attribute reference | |
await exp | Await expression | |
** | Exponentiation | |
+num , -num , ~num | Positive, negative, bitwise NOT | |
* , @ , / , // , % | Multiplication, matrix multiplication, division, floor division, remainder | |
+ , - | Addition and subtraction | |
<< , >> | Shifts | |
& | Bitwise AND | |
^ | Bitwise XOR | |
| | Bitwise OR | |
in , not in , is , is not , < , <= , > , >= , != , == | Comparisons, including membership tests and identity tests | |
not exp | Boolean NOT | |
and | Boolean AND | |
or | Boolean OR | |
if – else | Conditional expression | |
lambda | Lambda expression | |
:= | Assignment expression | lowest |
Please calculate the circumference and area of a circle.
The formula for the circumference of a circle is
The formula for the area of a circle is
PI:float = 3.141592
radius:float = 15
perimeter:float = 0 # Modify the code here to calculate the circumference
area:float = 0 # Modify the code here to calculate the area
print("The perimeter and area of a circle with a radius of", radius, "are", perimeter, "and", area)
Created in 5/15/2025
Updated in 5/21/2025